package com.venta.autopartes.logueo;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.venta.autopartes.dao.UserDAO;
import com.venta.autopartes.user.User;
import com.venta.autopartes.utils.Constants;

public class LoginAction extends ActionSupport implements SessionAware{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String userName;
	private String password;
	private Map session;

	public String authenticate(){
		UserDAO userDAO = new UserDAO();
		User userVO = null;
		
		System.out.println("We are in the LoginAction.authenticate method. Credentials: " +userName + " - " + password);
		
		userVO = userDAO.getUserLogin(getUserName(), getPassword());
		if (userVO != null) {
			
			System.out.println("Login success --> " + userVO.getUserId() + " - " +userVO.getUser());
			session.put(Constants.USER_LOGGED, userVO);
			
			session.put(Constants.SESSION_TYPE, userVO.getUserPrivilage().getUserPrivilage());
			userDAO.updateLastLoginDateByUser(userVO);
			
			return "success";
		} else {
			addActionError(getText("inicio.error.login"));
			return "error";
		}	
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;

	}

	public Map getSession() {
		return session;
	}

	public void setSession(Map session) {
		this.session = session;
	}
	
	
	
}
